

#include "Public.h"


UR_SEND_CMD_E ur_s_cmd;

UR_SEND_CMD_E ur_s_cmd_backup;

UR_SEND_STATUS_E ur_s_status;



idata u8 ur_send_para[UR_SEND_PARA_LEN];

idata u8 ur_send_buf[UR_SEND_BUFFER_LEN];

idata u8 ur_send_buf_len;

idata u8 ur_retry_send_cnt;

idata u8 ur_send_timer;

idata u8 auth_send_interval_timer;

UR_SEND_STATUS_E ur_get_send_status(void)
{
	return ur_s_status;
}

void ur_set_send_status(UR_SEND_STATUS_E st)
{
	ur_s_status = st;
}


void ur_implement_timer(void)
{
	if(ur_send_timer && ur_send_timer!=0xFF) ur_send_timer--;

	if(auth_send_interval_timer && auth_send_interval_timer!=0xFF) auth_send_interval_timer--;
}


void ur_implement_init(void)
{
	ur_fifo_init();

	ur_analyse_init();

	ur_s_cmd = UR_SEND_CMD_NONE;

	ur_s_status = UR_SEND_STATUS_IDLE;

	ur_send_timer = 0xFF;
	
}

void ur_implement_loop(void)
{
	ur_analyse_data();

	ur_implement_send_task();
}

void ur_send_cmd(UR_SEND_CMD_E cmd,u8 *para,u8 para_len)
{
	if(ur_s_status == UR_SEND_STATUS_IDLE)
	{
		ur_s_cmd = cmd;

		memcpy(ur_send_para,para,para_len);

		ur_retry_send_cnt = 0;
	}
}

void ur_calculate_checksum(u8 *cs,u8 *src,u8 src_len)
{
	u8 i;
	u16 sum = 0;
	for(i=0;i<src_len;i++)
		sum += *(src+i);
	
	*cs = ((sum%100)/10) + '0';
	*(cs+1) = (sum%10) + '0';
}

void ur_implement_send(void)
{
	if(ur_s_cmd == UR_SEND_CMD_NONE) return;
		switch(ur_s_cmd)
		{
			case UR_SEND_CMD_SET_MODE: 
				ur_send_buf[0] = 'A';
				ur_send_buf[1] = 'T';
				ur_send_buf[2] = 'B';
				ur_send_buf[3] = ur_send_para[0]+'0';
				ur_calculate_checksum(&ur_send_buf[4],&ur_send_buf[2],2);
				ur_send_buf[6] = UR_FIRST_END_FLAG;
				ur_send_buf[7] = UR_SECOND_END_FLAG;
				ur_send_buf_len = 8;
				break;

			
			case UR_SEND_CMD_LOGIN:
				ur_send_buf[0] = 'A';
				ur_send_buf[1] = 'T';
				ur_send_buf[2] = 'H';
				//dec_to_decascii(ur_send_para,buff_money,NUM_MOMEY,0);
				memcpy(&ur_send_buf[3],ur_send_para,NUM_MOMEY);
				ur_calculate_checksum(&ur_send_buf[8],&ur_send_buf[2],6);
				ur_send_buf[10] = UR_FIRST_END_FLAG;
				ur_send_buf[11] = UR_SECOND_END_FLAG;
				ur_send_buf_len = 12;
				break;

			case UR_SEND_CMD_VERSION:
				ur_send_buf[0] = 'A';
				ur_send_buf[1] = 'T';
				ur_send_buf[2] = 'G';
				//dec_to_decascii(ur_send_para,buff_money,NUM_MOMEY,0);
				memcpy(&ur_send_buf[3],ur_send_para,3);
				ur_calculate_checksum(&ur_send_buf[6],&ur_send_buf[2],4);
				ur_send_buf[8] = UR_FIRST_END_FLAG;
				ur_send_buf[9] = UR_SECOND_END_FLAG;
				ur_send_buf_len = 10;
				break;
			case UR_SEND_CMD_SET_CONSUME_MODE:
				ur_send_buf[0] = 'A';
				ur_send_buf[1] = 'T';
				ur_send_buf[2] = 'M';
				memcpy(&ur_send_buf[3],ur_send_para,UR_SEND_CMD_SET_CONSUME_MODE_LEN);
				ur_calculate_checksum(&ur_send_buf[4],&ur_send_buf[2],1 + UR_SEND_CMD_SET_CONSUME_MODE_LEN);
				ur_send_buf[6] = UR_FIRST_END_FLAG;
				ur_send_buf[7] = UR_SECOND_END_FLAG;
				ur_send_buf_len = 8;
			default:
				break;
		}		
	Uart_Send_String(ur_send_buf,ur_send_buf_len);
	//delay_ms(10);

//	key_num = KEY_CODE_NONE;
	memset(buff_money,0,sizeof(u8)*NUM_MOMEY);

	
	ur_s_cmd_backup = ur_s_cmd;

	ur_s_status = UR_SEND_STATUS_SENDED_AND_WAIT_OK;

	ur_send_timer = UR_SEND_RETRY_TIME;
	
	ur_s_cmd = UR_SEND_CMD_NONE;
}


void ur_implement_send_task(void)
{
	if(ur_send_timer==0)
	{
		if(++ur_retry_send_cnt<3)
		{
			ur_s_cmd = ur_s_cmd_backup;

			ur_send_timer = UR_SEND_RETRY_TIME;
		}
		else
		{
			ur_retry_send_cnt = 0;

			ur_send_timer = 0xFF;

			ur_s_status = UR_SEND_STATUS_IDLE;
		}
	}
	ur_implement_send();
}

void ur_implement_send_ok(void)
{
	ur_send_buf[0] = 'A';
	ur_send_buf[1] = 'T';
	ur_send_buf[2] = 'O';
	ur_send_buf[3] = 'K';
	ur_send_buf[4] = UR_FIRST_END_FLAG;
	ur_send_buf[5] = UR_SECOND_END_FLAG;
	ur_send_buf_len = 6;
	Uart_Send_String(ur_send_buf,ur_send_buf_len);
}


u8 ur_checksum_if_is_ok(u8 *frame,u8 len)
{
	u8 checksum[2];

	u8 ret;

	ur_calculate_checksum(checksum,frame,len);

	//Uart_Send_String(checksum,2);
	
	if(!memcmp(checksum,&frame[len],2))
	{
		ret = 1;
	}
	else
	{
		ret = 0;
	}
	return ret;
}
